package com.zlzlib.nettytest;

import android.util.Log;

import com.zlzlib.libnetty.listener.NettyConnectStateListener;
import com.zlzlib.libnetty.ws.WebSocketClient;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Vector;

/**
 * @DateTime: 2022/11/2 15:42
 * @Author zlz
 * @Version 1.0
 */
public class TestWeb {

    private volatile static TestWeb instance = null;

    public static TestWeb getInstance() {
        if (instance == null) {
            synchronized (TestWeb.class) {
                if (instance == null) {
                    instance = new TestWeb();
                }
            }
        }
        return instance;
    }

    private TestWeb() {
        client =new WebSocketClient();
    }

    private final WebSocketClient client;

    public void init() {
        new Thread(() -> {
            Vector<String> vector = new Vector<>();
            vector.add("ws://192.168.0.109:23334/zlz_delivery1");
            client.addHeader("key", getLocalHostIp());
            client.init(vector, new DemoNettyEven(), new NettyConnectStateListener() {
                @Override
                public void onConnecting() {
                    Log.i(getClass().getName(), "onConnecting: ");
                }

                @Override
                public void onConnected() {
                    Log.i(getClass().getName(), "onConnected: ");
                }

                @Override
                public void onConnectFailed() {
                    Log.i(getClass().getName(), "onConnectFailed: ");
                }

                @Override
                public void onConnectClose() {

                }

                @Override
                public void onConnectException(Throwable e) {

                }
            }, new DemoNettyData());
        }).start();
    }

    public void restart() {
        new Thread(() -> client.resetConnect(true)).start();
    }

    public void close() {
        client.close();
    }

    public void send(String msg) {
        client.sendMsg(msg);
    }


    private String getLocalHostIp() {
        String ipAddress = "";
        try {
            Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
            // 遍历所用的网络接口
            while (en.hasMoreElements()) {
                NetworkInterface nif = en.nextElement();// 得到每一个网络接口绑定的所有ip
                Enumeration<InetAddress> inet = nif.getInetAddresses();
                // 遍历每一个接口绑定的所有ip
                while (inet.hasMoreElements()) {
                    InetAddress ip = inet.nextElement();
                    if (!ip.isLoopbackAddress() && ip instanceof Inet4Address) {
                        ipAddress = ip.getHostAddress();
                        break;
                    }
                    ip = null;
                }
                if (!ipAddress.isEmpty()) {
                    break;
                }
                nif = null;
                inet = null;
            }
            en = null;
        } catch (SocketException e) {
            ipAddress = "";// 为空将会返回获取主机ip失败的错误信息
        }
        return ipAddress;
    }
}
